package com.wdb.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wdb.srv.UserService;

/**
 * @author ld
 * @date 3, 11, 2015
 * @blog http://localhost:8080/WDB/login?username=?&password=?
 */
public class CollectServlet extends HttpServlet {

	private static final long serialVersionUID = -7715894432269979527L;
	private UserService us;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("CollectServlet 被创建！");
		super.destroy();
	}

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		System.out.println("CollectServlet 被销毁！");
		super.init();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=UTF-8");
	}

	@SuppressWarnings("unused")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession hs = request.getSession();
		Integer uid = (Integer) hs.getAttribute("uid");

		int pid = Integer.valueOf(request.getParameter("pid"));
		System.out.println(pid);

		try {
			us = new UserService();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		} catch (SQLException e) {
			e.printStackTrace();
			return;
		}
		if (uid != null) {
			// 收藏
			us.doCollect(uid.intValue(), pid);
		} else if (uid != null && pid == -1) {
			try {
				us.getAllCollect(uid.intValue());
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

		response.sendRedirect("./mycollects.jsp");
	}
}
